#include "RFTestFunction.h"
#include "CC1101.h"
#include "tools.h"
#include "RF24L01.h"
#include "String.h"

/*this function is frequent test for CC1101*/
//void frequentTest(void)
//{
//    uint8_t ch = 165, changeCH[3] = {0xAA, 0x55, 0x00};
//    uint8_t msg[4] = {0x55, 0xAA }, origin[64], originLen = 0;
//    uint16_t count = 0, err = 0, ok = 0, countInMsg = 0, countStart = 0;
//    uint32_t start;
//    CC1101_Clear_RxBuffer;
//    CC1101_Set_CH(ch);
//    CC1101_Rx_Mode;
//    //record the start time
//    start = HAL_GetTick();
//    //wait for receive signal or timeout
//    while((CC1101_Read_Status(CC1101_MARCSTATE) != STATE_IDLE) && ((HAL_GetTick() - start <= 1000)));
//    if(CC1101_Read_Status(CC1101_MARCSTATE) == STATE_IDLE)
//    {
//        ///////////////////////////////////////////////////////////////////////////////////////////////////////////
//        //msg received, enter RX mode
//        while(1)
//        {
//            //wait msg
//            CC1101_Clear_RxBuffer;
//            led_off;
//            CC1101_Rx_Mode;
//            while(CC1101_Read_Status(CC1101_MARCSTATE) != STATE_IDLE);
//            led_on;
//            clear_screen();
//            //read msg
//            originLen = CC1101_Get_RxCounter;
//            CC1101_Read_Multi_Reg(CC1101_RXFIFO, origin, originLen);
//            //print msg
//            for(uint8_t i = 0; i < originLen; i++)
//            {
//                print_Hex1(0, i * 12 + i * 2, origin[i], OLED_HALF);
//            }
//            //check msg length, must be 3 or 4
//            if(origin[0] != 3 && origin[0] != 4)
//            {
//                err++;
//                continue;
//            }
//            //check paload length, must be msg length + 3
//            if(originLen != origin[0] + 3)
//            {
//                err++;
//                continue;
//            }
//            //read CRC_OK flag
//            if((origin[origin[0] + 3 - 1] & CRC_OK) == 0)
//            {
//                err++;
//                continue;
//            }
//            //anysics msg
//            switch(origin[0])
//            {
//            // change frequent msg
//            case 3:
//                //check first two bytes
//                if(origin[1] != 0xAA || origin[2] != 0x55)
//                {
//                    err++;
//                    continue;
//                }
//                //change frequent
//                CC1101_Set_CH(origin[3]);
//                err = 0;
//                ok = 0;
//                countInMsg = 0;
//                countStart = 0;
//                //record chanal
//                ch =  origin[3];
//                break;
//            // normal msg
//            case 4:
//                //check first two bytes
//                if(origin[1] != 0x55 || origin[2] != 0xAA)
//                {
//                    err++;
//                    continue;
//                }
//                //the correct msg counter plus 1
//                ok++;
//                //if never record the time when receive the first msg, record it.
//                if(countStart == 0)
//                {
//                    countStart = *((uint16_t*)(origin + 3));
//                }
//                countInMsg = *((uint16_t*)(origin + 3));
//                //print ch
//                display_string_5x7(1, 0, (uint8_t*)"CH:");
//                print_byte(1, 40, ch, OLED_HALF);
//                //print RSSI
//                display_string_5x7(2, 0, (uint8_t*)"RSSI:");
//                print_char(2, 40, CC1101_Read_Status(CC1101_RSSI), OLED_HALF);
//                //print correct msg counter
//                display_string_5x7(3, 0, (uint8_t*)"OK:");
//                print_uint16(3, 40, ok, OLED_HALF);
//                //print the count of msg has send.
//                display_string_5x7(4, 0, (uint8_t*)"Send:");
//                print_uint16(4, 40, countInMsg + 1 - countStart, OLED_HALF);
//                break;
//            }
//        }
//    }
//    else
//    {
//        //////////////////////////////////////////////////////////////////////////////////////////////////////////////
//        //no msg received ,enter TX
//        count = 0;
//        while(1)
//        {
//            count++;
//            //write the msg count in to msg
//            msg[2] = (uint8_t)count;
//            msg[3] = (uint64_t)(count >> 8);
//            //sending
//            led_on;
//            CC1101_Tx_Packet(4, msg);
//            //wait for sending commplite
//            while(CC1101_Read_Status(CC1101_MARCSTATE) != STATE_IDLE);
//            led_off;
//            //print msg count
//            display_GB2312_string(0, 0, (uint8_t*)"Count:");
//            print_uint16(0, 50, count, OLED_FULL);
//            //print ch
//            display_GB2312_string(2, 0, (uint8_t*)"CH:");
//            print_byte(2, 50, ch, OLED_FULL);
//            //change ch when key pressed
//            if(key1_check() == KEY_DOWN)
//            {
//                //ch plus 1
//                ch++;
//                //count??
//                count = 0;
//                changeCH[2] = ch;
//                //tell reciever change ch
//                CC1101_Tx_Packet(3, changeCH);
//                //wait
//                while(CC1101_Read_Status(CC1101_MARCSTATE) != STATE_IDLE);
//                //change ch
//                CC1101_Set_CH(ch);
//                //delay 2000ms
//                start = HAL_GetTick();
//                while(HAL_GetTick() - start <= 2000)
//                {
//                    led_invert;
//                    HAL_Delay(50);
//                }
//                led_off;
//            }
//            HAL_Delay(10);
//        }
//    }
//}

void NRF24L01_Comulication_Test( void )
{
    const char *pMsg = "awdesq";
    uint16_t count = 0;
    uint32_t start = 0;
    uint8_t observe = 0;
    uint16_t error_count = 0;
    //init nrf24l01, power up going to standby mode
    NRF24L01_Init();
    if(*(uint8_t*)CPUID == 0x32)
    {
        display_GB2312_string(0, 0, (uint8_t*)"Tx Mode");
        display_GB2312_string(6, 0, (uint8_t*)"ReTry:");
        display_GB2312_string(4, 0, (uint8_t*)"Error:");
        while(1)
        {
            //clear
            NRF24L01_Flush_Tx_Fifo();
            NRF24L01_Clear_IRQ_Flag(IRQ_ALL);
            NRF24L01_Write_Tx_Payload_Ack((uint8_t*)pMsg, strlen(pMsg));
            //start TX
            led_on;
            NRF24L01_Trig_Tx();
            start = HAL_GetTick();
            //wait
            while(HAL_GetTick() - start < 200)
            {
                if(NRF24L01_Read_IRQ_Status(TX_DS | MAX_RT) & ( TX_DS | MAX_RT))
                {
                    break;
                }
            }
            //print count
            count++;
            print_uint16(2, 0, count, OLED_FULL);
            led_off;
            //print observe
            observe = NRF24L01_Read_Reg(OBSERVE_TX);
            print_byte(4, 50, observe >> 4, OLED_FULL);
            print_byte(6, 50, observe & 0x0F, OLED_FULL);
            print_uint16(4, 80, error_count, OLED_FULL);
            //lost packet count incress to max value
            if((observe & 0xF0) == 0xF0)
            {
                NRF24L01_Write_Reg(RF_CH, NRF24L01_Read_Reg(RF_CH));
                error_count += 15;
            }
            HAL_Delay(10);
        }
    }
    else
    {
        display_GB2312_string(0, 0, (uint8_t*)"Rx Mode");
        //set RX mode, Will in Standby mode while CE = 0
        NRF24L01_Set_Mode(MODE_RX);
        //enter RX mode, wait Tpd2stby PowerDown->Standby time
        RF24L01_SET_CE_HIGH;
        HAL_Delay(5);
        // Flush Rx FIFO
        NRF24L01_Flush_Rx_Fifo();
        while(1)
        {
            uint8_t buf[32], length;
            //RX_DR will be set if has received a packet
            while(NRF24L01_Read_IRQ_Status(RX_DR) != RX_DR);
            led_on;
            while(NRF24L01_Read_IRQ_Status(RX_DR) == RX_DR)
            {
                length = NRF24L01_Read_Top_Fifo_Width();
                NRF24L01_Read_Buf(RD_RX_PLOAD, buf, length);
                count++;
                print_uint16(2, 0, count, OLED_FULL);
                NRF24L01_Clear_IRQ_Flag(IRQ_ALL);
            }
            //????,????
            NRF24L01_Flush_Rx_Fifo();
            NRF24L01_Clear_IRQ_Flag(IRQ_ALL);
            led_off;
            count++;
            print_uint16(2, 0, count, OLED_FULL);
        }
    }
}


